using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace XmlObjects.XmlDocumentMarkup.XmlWriter
{
	public class ElementWriter : IXmlSimpleEntity
	{
		public void Write(XmlNode node, object value, string name)
		{
			//  Assume that if the 
			XmlDocument parentDocument = node.OwnerDocument;
			if (parentDocument == null)
				parentDocument = (XmlDocument)node;

			XmlNode theNode = node.SelectSingleNode(name);
			if (theNode==null)
			{
				theNode = parentDocument.CreateElement(name);
				node.AppendChild(theNode);
			}
			if (value!=null)
				theNode.InnerText = value.ToString();
		}

		public object Read(XmlNode node, string name)
		{
			XmlNode chileNode = node.SelectSingleNode(name);
			if (chileNode != null)
				return chileNode.InnerText;
			else
				return null;
		}
	}
}
